/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * RSGeometriesLibrary.cpp
 *
 *  Created on: 2010/02/16
 *      Author: Alexandre Pires ( c.alexandre.pires@gmail.com )
 */

#include "RSGeometriesLibrary.h"
#include "RSGeometry.h"

namespace Realsphere {

    RSGeometriesLibrary::RSGeometriesLibrary ( const QString& rId, const QString& rName ) :
        RSUIDElement ( rId, RS_GEOMETRIES_LIBRARY_TAG )
    {
        SetName ( rName, false );
    }

    RSGeometriesLibrary::~RSGeometriesLibrary ()
    {
        mGeometries.clear ();
    }

    RSGeometry* RSGeometriesLibrary::GetGeometry ( unsigned int rIndex ) const
    {
        return mGeometries[rIndex];
    }

    RSGeometry* RSGeometriesLibrary::GetGeometry ( const QString& rId ) const
    {
        RSGeometriesIterator iterator = RSGeometriesIterator ( mGeometries );

        while ( iterator.hasNext () )
        {
            RSGeometry* current_geometry = iterator.next ();
            QString geometry_id = current_geometry->Id ();

            if ( geometry_id == rId )
                return current_geometry;
        }

        return NULL;
    }

    int RSGeometriesLibrary::IndexOf ( RSGeometry* rGeometry )
    {
        return mGeometries.indexOf ( rGeometry );
    }

    unsigned int RSGeometriesLibrary::GeometriesSize ()
    {
        return mGeometries.size ();
    }

    RSGeometries RSGeometriesLibrary::Geometries () const
    {
        return mGeometries;
    }

    RSGeometriesIterator RSGeometriesLibrary::GeometriesIterator () const
    {
        return RSGeometriesIterator ( mGeometries );
    }

    bool RSGeometriesLibrary::IsRootElement () const
    {
        return false;
    }

    RSError RSGeometriesLibrary::BeforeAddChildElement ( RSElement* rChild )
    {
        RSError error = RSElement::BeforeAddChildElement ( rChild );

        if ( error == RS_INVALID_ELEMENT )
        {

            if ( rChild->InstanceOf ( RS_GEOMETRY_CLASS ) )
                error = RS_NO_ERROR;

        }

        return error;
    }

    bool RSGeometriesLibrary::AfterAddChildElement ( RSElement* rChild )
    {
        bool handled = RSElement::AfterAddChildElement ( rChild );

        if ( !handled && rChild->InstanceOf ( RS_GEOMETRY_CLASS ) )
        {

            RSGeometry* geometry = RSGeometryCast ( rChild );
            mGeometries.append ( geometry );
            handled = true;

            emit GeometryAdded ( this, geometry );
        }

        return handled;
    }

    bool RSGeometriesLibrary::AfterRemoveChildElement ( RSElement* rChild )
    {
        bool handled = RSElement::AfterRemoveChildElement ( rChild );

        if ( !handled && rChild->InstanceOf ( RS_GEOMETRY_CLASS ) )
        {
            RSGeometry* geometry = RSGeometryCast ( rChild );
            int index = mGeometries.indexOf ( geometry );
            mGeometries.removeAt ( index );
            handled = true;

            emit GeometryRemoved ( this, geometry );
        }

        return handled;
    }

    RSError RSGeometriesLibrary::AddGeometry ( RSGeometry* rGeometry )
    {
        RSError error = AddChildElement ( rGeometry );
        return error;
    }

    RSError RSGeometriesLibrary::RemoveGeometry ( RSGeometry* rGeometry )
    {
        RSError error = RemoveChildElement ( rGeometry );
        return error;
    }

    RSError RSGeometriesLibrary::RemoveGeometry ( const QString& rId )
    {
        RSGeometry* geometry = GetGeometry ( rId );
        RSError error = RemoveGeometry ( geometry );
        return error;
    }

    RSError RSGeometriesLibrary::RemoveGeometry ( unsigned int rIndex )
    {
        RSGeometry* geometry = GetGeometry ( rIndex );
        RSError error = RemoveGeometry ( geometry );
        return error;
    }
}
